﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChoWorkflowServer.Model
{
    [Serializable]
    public class ChoTaskMsgPayload : ChoCommand
    {
        public int TaskId { get; set; }
        public int BatchId { get; set; }
        public string TaskName { get; set; }
        public string ScheduleName { get; set; }
        public int TaskStatus { get; set; }
        public string TaskStatusDesc { get; set; }
        public string TaskDesc { get; set; }
        public int TaskGroupId { get; set; }
        public string TaskGroupDesc { get; set; }
        public string CommandParameters { get; set; }
        public string BatchCommandParameters { get; set; }
        public string LogMessage { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
        public TimeSpan Duration { get; set; }
        public string NextRunTime { get; set; }
        public int RetryCount { get; set; }
        public string MachineName { get; set; }
        public string BatchMachineName { get; set; }
        public string User { get; set; }
        public DateTime ModifiedDate { get; set; }
        public int ExitCode { get; set; }

        public void ApplyChanges(ChoTasksDataSet.CHO_TASKSDataTable table)
        {
            //ChoTasksDataSet.CHO_TASKSRow x = (from dataRow in table.AsEnumerable()
            //                                         where dataRow.TASK_ID == TaskId
            //                                         && dataRow.SCHEDULE_NAME == ScheduleName
            //                                         //&& dataRow.MODIFIED_DATE < ModifiedDate
            //                                         select dataRow).FirstOrDefault();

            ChoTasksDataSet.CHO_TASKSRow x = table.Rows.Find(new object[] { TaskId, ScheduleName }) as ChoTasksDataSet.CHO_TASKSRow;
            if (x != null)
                ApplyChanges(x);
        }

        public void ApplyChanges(ChoTasksDataSet.CHO_TASKSRow row)
        {
            row.BeginEdit();
            row.BATCH_ID = BatchId;
            row.TASK_STATUS = TaskStatus;
            row.TASK_STATUS_DESC = TaskStatusDesc;
            row.TASK_DESC = TaskDesc;
            row.TASK_GRP_DESC = TaskGroupDesc;
            row.CMD_PARAMETERS = CommandParameters;
            row.BATCH_CMD_PARAMETERS = BatchCommandParameters;
            row.LOG_MSG = LogMessage;
            row.START_TIME = StartTime;
            if (EndTime == DateTime.MinValue)
                row.SetEND_TIMENull();
            else
                row.END_TIME = EndTime;
            if (Duration == TimeSpan.Zero)
                row.SetEND_TIMENull();
            else
                row.DURATION = Duration.ToString();
            row.NEXT_RUN_TIME = NextRunTime;
            row.RETRY_COUNT = RetryCount;
            row.MACHINE_NAME = MachineName;
            row.BATCH_MACHINE_NAME = BatchMachineName;
            row.MODIFIED_BY = User;
            row.MODIFIED_DATE = ModifiedDate;
            row.EXIT_CODE = ExitCode;
            row.EndEdit();
        }

        public static ChoTaskMsgPayload New(ChoTasksDataSet.CHO_TASKSRow row)
        {
            ChoTaskMsgPayload taskMsgPayload = new ChoTaskMsgPayload();
            taskMsgPayload.TaskId = row.TASK_ID;
            taskMsgPayload.BatchId = row.BATCH_ID;
            taskMsgPayload.TaskName = row.TASK_NAME;
            taskMsgPayload.ScheduleName = row.IsSCHEDULE_NAMENull() ? String.Empty : row.SCHEDULE_NAME;
            taskMsgPayload.TaskStatus = row.TASK_STATUS;
            taskMsgPayload.TaskStatusDesc = row.TASK_STATUS_DESC;
            if (!row.IsTASK_DESCNull())
                taskMsgPayload.TaskDesc = row.TASK_DESC;
            if (!row.IsTASK_GRP_IDNull())
                taskMsgPayload.TaskGroupId = row.TASK_GRP_ID;
            if (!row.IsTASK_GRP_DESCNull())
                taskMsgPayload.TaskGroupDesc = row.TASK_GRP_DESC;
            if (!row.IsCMD_PARAMETERSNull())
                taskMsgPayload.CommandParameters = row.CMD_PARAMETERS;
            if (!row.IsBATCH_CMD_PARAMETERSNull())
                taskMsgPayload.BatchCommandParameters = row.BATCH_CMD_PARAMETERS;
            if (!row.IsLOG_MSGNull())
                taskMsgPayload.LogMessage = row.LOG_MSG;
            if (!row.IsSTART_TIMENull())
                taskMsgPayload.StartTime = row.START_TIME;
            if (!row.IsEND_TIMENull())
                taskMsgPayload.EndTime = row.END_TIME;
            if (!row.IsDURATIONNull())
                taskMsgPayload.Duration = TimeSpan.ParseExact(row.DURATION, "c", null);
            if (!row.IsNEXT_RUN_TIMENull())
                taskMsgPayload.NextRunTime = row.NEXT_RUN_TIME;
            if (!row.IsRETRY_COUNTNull())
                taskMsgPayload.RetryCount = row.RETRY_COUNT;
            taskMsgPayload.MachineName = row.MACHINE_NAME;
            if (!row.IsBATCH_MACHINE_NAMENull())
                taskMsgPayload.BatchMachineName = row.BATCH_MACHINE_NAME;
            if (!row.IsMODIFIED_BYNull())
                taskMsgPayload.User = row.MODIFIED_BY;
            if (!row.IsMODIFIED_DATENull())
                taskMsgPayload.ModifiedDate = row.MODIFIED_DATE;
            if (!row.IsEXIT_CODENull())
                taskMsgPayload.ExitCode= row.EXIT_CODE;

            return taskMsgPayload;
        }
    }
}
